package com.wms.controller;

import com.wms.dto.LogisticsPriceDto;
import com.wms.pojo.LogisticsPrice;
import com.wms.pojo.PageResult;
import com.wms.pojo.Result;
import com.wms.service.LogisticsPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/logisticsprice")
public class LogisticsPriceController {
    @Autowired
    private LogisticsPriceService logisticsPriceService;

    @GetMapping
    public Result querypage(Integer page,Integer pageSize,String companyName,Integer category){
        PageResult pageResult = logisticsPriceService.querypage(page,pageSize,companyName,category);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result saveLogisticsPrice(@RequestBody LogisticsPriceDto dto){
        LogisticsPrice logisticsPrice = LogisticsPrice.builder().logisticsCompanyId(dto.getCompanyName())
                .kilogramPrice(dto.getKilogramPrice())
                .cubePrice(dto.getCubePrice())
                .transKilometerPrice(dto.getTransKilometerPrice())
                .category(dto.getCategory())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        logisticsPriceService.save(logisticsPrice);
        return Result.success();
    }

    //回显
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        LogisticsPrice logisticsPrice = logisticsPriceService.getById(id);
        LogisticsPriceDto logisticsPriceDto = LogisticsPriceDto.builder().id(logisticsPrice.getId())
                .transKilometerPrice(logisticsPrice.getTransKilometerPrice())
                .kilogramPrice(logisticsPrice.getKilogramPrice())
                .cubePrice(logisticsPrice.getCubePrice())
                .category(logisticsPrice.getCategory())
                .companyName(logisticsPrice.getLogisticsCompanyId())
                .build();
        return Result.success(logisticsPriceDto);
    }

    //修改
    @PutMapping
    public Result updateById(@RequestBody LogisticsPriceDto dto){
        LogisticsPrice logisticsPrice = LogisticsPrice.builder()
                .id(dto.getId())
                .logisticsCompanyId(dto.getCompanyName())
                .kilogramPrice(dto.getKilogramPrice())
                .cubePrice(dto.getCubePrice())
                .transKilometerPrice(dto.getTransKilometerPrice())
                .category(dto.getCategory())
                .updateTime(LocalDateTime.now())
                .build();
        logisticsPriceService.updateById(logisticsPrice);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        logisticsPriceService.removeById(id);
        return Result.success();
    }
}
